package com.jn.listtest_2.adapter;

import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;


import com.jn.listtest_2.R;
import com.jn.listtest_2.bean.Student;

import java.util.List;

public class StudentRecyclerAdapter extends RecyclerView.Adapter<StudentRecyclerAdapter.ViewHolder> {

    private List<Student> mList;
    private OnItemClickListener mListener;
    public StudentRecyclerAdapter(List<Student> list, OnItemClickListener listener) {
        Log.e("test123", "====StudentRecyclerAdapter=====" + list.size());
        mList = list;
        mListener = listener;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.recycler_item, parent, false);
        ViewHolder viewHolder = new ViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        Student student = mList.get(position);
        Log.e("test123", "====onBindViewHolder====="+student);
        holder.nameText.setText(student.getName());
        holder.numberText.setText(student.getNumber());
        holder.main.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mListener.onItemClick(student);
            }
        });
    }

    @Override
    public int getItemCount() {
        return mList.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        LinearLayout main;
        TextView nameText;
        TextView numberText;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            main = itemView.findViewById(R.id.main);
            nameText = itemView.findViewById(R.id.name);
            numberText = itemView.findViewById(R.id.number);
        }
    }

    public interface OnItemClickListener{
        void onItemClick(Student student);
    }
}